package com.cellulam.framework.core.model;

import com.cellulam.framework.core.enums.JdbcType;
import com.cellulam.framework.core.enums.Operator;
import com.cellulam.framework.core.enums.SortType;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Map;

@Getter
public class PagerCriteria<T> {

    /**
     * 默认limit
     */
    public static final int DEFAULT_LIMIT = 20;

    /**
     * 默认offset
     */
    public static final int DEFAULT_OFFSET = 0;

    /**
     * 默认排序
     */
    public static final SortType DEFAULT_SORT_TYPE = SortType.DESC;

    public PagerCriteria(int limit, int offset) {
        this.limit = limit > 0 ? limit : DEFAULT_LIMIT;
        this.offset = offset >= 0 ? offset : DEFAULT_OFFSET;

        this.sort = Maps.newHashMap();
    }

    public PagerCriteria(T criteria) {
        this(DEFAULT_LIMIT, DEFAULT_OFFSET);
        this.setCriteria(criteria);
    }

    /**
     * limit
     */
    private int limit;

    /**
     * offset
     */
    private int offset;

    /**
     * 排序
     * key: 排序字段，value: 排序方式(SortType, 1: 正序，2: 倒序)
     * 废弃，多个字段排序会有问题
     */
    @Deprecated
    private Map<String, Integer> sort;

    private List<PagerSort> sortFields;

    /**
     * 自定义查询条件
     */
    private List<PagerCondition> conditions;

    public void setLimit(int limit) {
        this.limit = limit;
    }

    /**
     * 查询条件
     */
    private T criteria;

    public void setCriteria(T criteria) {
        this.criteria = criteria;
    }

    /**
     * 添加自定义查询条件
     *
     * @param field
     * @param value
     */
    public void addCondition(String field, Object value) {
        this.addCondition(field, value, Operator.EQUAL, JdbcType.VARCHAR.getCode());
    }


    /**
     * 添加自定义查询条件
     *
     * @param field
     * @param value
     * @param operator
     * @param jdbcType
     */
    public void addCondition(String field, Object value, Operator operator, int jdbcType) {
        this.addCondition(field, value, operator.getCode(), jdbcType);
    }

    /**
     * 添加自定义查询条件
     *
     * @param field
     * @param value
     * @param operator
     * @param jdbcType
     */
    public void addCondition(String field, Object value, int operator, int jdbcType) {
        if (this.conditions == null) {
            this.conditions = Lists.newArrayList();
        }
        this.conditions.add(PagerCondition.builder()
                .field(field)
                .value(value)
                .operator(operator)
                .jdbcType(jdbcType)
                .build());
    }

    public boolean addSort(String field, SortType sortType) {
        if (StringUtils.isEmpty(field)) {
            return false;
        }
        if (sortType == null) {
            sortType = DEFAULT_SORT_TYPE;
        }

        if (this.sortFields == null) {
            this.sortFields = Lists.newLinkedList();
        }

        this.sort.put(field, sortType.getCode());
        this.sortFields.add(PagerSort.builder()
                .field(field)
                .sortType(sortType.getCode())
                .build());
        return true;
    }

    public boolean addSort(String field, Integer sortType) {
        return addSort(field, SortType.valueOf(sortType));
    }

}
